import { filter, FilterTransform } from '.';
import Vue from 'vue';

@filter('date-filter')
export default class DateFilter extends Vue implements FilterTransform {
  name:string="111";
  transform(value: any, ...args: any[]): String {
    console.log(this);
    let aaa= this.timeAgo(value);
    return aaa;
  }
  mode:string='111';
  timeAgo(time: any) {

    const between = Date.now() / 1000 - Number(new Date(time))/1000;
    if (between < 3600) {
      return this.pluralize(~~(between / 60), ' 分钟')
    } else if (between < 86400) {
      return this.pluralize(~~(between / 3600), ' 小时')
    } else {
      return this.pluralize(~~(between / 86400), ' 天')
    }
  }
  pluralize(time: number, label: string) {
    if (time === 1) {
      return time + label+"前";
    }
    return time + label + "前";
  }
  /* 数字 格式化*/

  numberFormatter(num: number, digits: number) {
    const si = [
      { value: 1E18, symbol: 'E' },
      { value: 1E15, symbol: 'P' },
      { value: 1E12, symbol: 'T' },
      { value: 1E9, symbol: 'G' },
      { value: 1E6, symbol: 'M' },
      { value: 1E3, symbol: 'k' }
    ]
    for (let i = 0; i < si.length; i++) {
      if (num >= si[i].value) {
        return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
      }
    }
    return num.toString()
  }
  toThousandFilter(num) {
    return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  }

}





